package com.ymatou.shoppingcart.api.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.shoppingcart.api.parameter.ChangeCatalogSelectedBean;
import com.ymatou.shoppingcart.api.service.ChangeCatalogSelectedCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.database.mongo.ShopCartMongoBean;
import com.ymttest.database.mongo.ShoppingCartMongoWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 修改购物车选中状态 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ChangeCatalogSelected {
	private static ChangeCatalogSelectedBean changebean;
	private static ChangeCatalogSelectedCall changecall;
	ShoppingCartMongoWapper scw;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改购物车选中状态");
	}

	@Before
	public void caseUp() {
		changebean = new ChangeCatalogSelectedBean();
		changecall = new ChangeCatalogSelectedCall();
		try {
			scw = new ShoppingCartMongoWapper();
		} catch (Exception e) {
			Logger.setup("MongoDB初始化失败：" + e.getMessage(), -1);
		}
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(changecall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	//TODO 购物车表和结构都有修改，case需要对应调整
	/*@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeCatalogSelected_001() {
		Logger.start(true,"修改购物车选中状态为未选中成功");
		try {
			Logger.comment("从mongodb中随机获取一条购物车记录");

			ShopCartMongoBean re = scw.getRandomShoppingCartInfo();
			Logger.comment("setCartId:" + re.get_id());
			changebean.setCartId(re.get_id());
			Logger.comment("setIsChecked:" + false);
			changebean.setIsChecked("false");
			Logger.comment("setUserId:" + re.getUid());
			changebean.setUserId(re.getUid());
			changecall.setData(changebean);
			changecall.callService();
			Logger.comment("changecall send:" + changecall.getEntityString());
			Logger.comment("changecall return:" + changecall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, changecall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, changecall.getBCode(), "验证返回bcode");
			ShopCartMongoBean scb = scw.getShoppingCartInfoList("{\"_id\":\"" + re.get_id() + "\"}", 0).get(0);
			Logger.verifyEquals("false", scb.getCheck(), "验证返回bcode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeCatalogSelected_002() {
		Logger.start(true,"不输入必填项CartId，修改购物车选中状态失败");
		try {
			Logger.comment("从mongodb中随机获取一条购物车记录");

			ShopCartMongoBean re = scw.getRandomShoppingCartInfo();
			// Logger.comment("setCartId:"+re.get_id());
			// changebean.setCartId(re.get_id());
			Logger.comment("setIsChecked:" + false);
			changebean.setIsChecked("false");
			Logger.comment("setUserId:" + re.getUid());
			changebean.setUserId(re.getUid());
			changecall.setData(changebean);
			changecall.callService();
			Logger.comment("changecall send:" + changecall.getEntityString());
			Logger.comment("changecall return:" + changecall.getReturnData());
			Logger.verifyEquals(ECode.ERROR500, changecall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, changecall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, changecall.getMsg().contains(ECode.ERROR500_MSG_4), "验证返回Msg是否包含字符:"
					+ ECode.ERROR500_MSG_4);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeCatalogSelected_003() {
		// IsChecked需要外部系统保证正确传入，购物车接口本身不做判断.如不传入默认false
		Logger.start(true,"不输入IsChecked，修改购物车选中状态成功");
		try {
			Logger.comment("从mongodb中随机获取一条购物车记录");

			ShopCartMongoBean re = scw.getRandomShoppingCartInfo();
			Logger.comment("setCartId:" + re.get_id());
			changebean.setCartId(re.get_id());
			// Logger.comment("setIsChecked:"+false);
			// changebean.setIsChecked("false");
			Logger.comment("setUserId:" + re.getUid());
			changebean.setUserId(re.getUid());
			changecall.setData(changebean);
			changecall.callService();
			Logger.comment("changecall send:" + changecall.getEntityString());
			Logger.comment("changecall return:" + changecall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, changecall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, changecall.getBCode(), "验证返回bcode");
			ShopCartMongoBean scb = scw.getShoppingCartInfoList("{\"_id\":\"" + re.get_id() + "\"}", 0).get(0);
			Logger.verifyEquals("false", scb.getCheck(), "验证返回bcode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeCatalogSelected_004() {
		Logger.start(true,"不输入必填项UserId，修改购物车选中状态失败");
		try {
			Logger.comment("从mongodb中随机获取一条购物车记录");

			ShopCartMongoBean re = scw.getRandomShoppingCartInfo();
			Logger.comment("setCartId:" + re.get_id());
			changebean.setCartId(re.get_id());
			Logger.comment("setIsChecked:" + false);
			changebean.setIsChecked("false");
			// Logger.comment("setUserId:"+re.getUid());
			// changebean.setUserId(re.getUid());
			changecall.setData(changebean);
			changecall.callService();
			Logger.comment("changecall send:" + changecall.getEntityString());
			Logger.comment("changecall return:" + changecall.getReturnData());
			Logger.verifyEquals(ECode.ERROR500, changecall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, changecall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, changecall.getMsg().contains(ECode.ERROR500_MSG_2), "验证返回Msg是否包含字符:"
					+ ECode.ERROR500_MSG_2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_ChangeCatalogSelected_005() {
		Logger.start(true,"输入UserId和CartId不匹配，修改购物车选中状态失败");
		try {
			Logger.comment("从mongodb中随机获取一条购物车记录");

			ShopCartMongoBean re = scw.getRandomShoppingCartInfo();
			Logger.comment("setCartId:" + re.get_id());
			changebean.setCartId(re.get_id());
			Logger.comment("setIsChecked:" + false);
			changebean.setIsChecked("false");
			// uid不匹配
			String uid = TestDataManager.getRandomUUID().toString();
			Logger.comment("setUserId:" + uid);
			changebean.setUserId(uid);
			changecall.setData(changebean);
			changecall.callService();
			Logger.comment("changecall send:" + changecall.getEntityString());
			Logger.comment("changecall return:" + changecall.getReturnData());
			Logger.verifyEquals(ECode.FAILED201, changecall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, changecall.getBCode(), "验证返回bcode");
			// 应该为 此购物车记录Id不属于当前用户 ？
			Logger.verifyEquals(true, changecall.getMsg().contains(ECode.FAILED201_MSG_4), "验证返回Msg是否包含字符:"
					+ ECode.FAILED201_MSG_4);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_ChangeCatalogSelected_006() {
		Logger.start(true,"输入不存在CartId，修改购物车选中状态失败");
		try {
			Logger.comment("从mongodb中随机获取一条购物车记录");

			ShopCartMongoBean re = scw.getRandomShoppingCartInfo();
			String cartid = TestDataManager.getRandomUUID().toString();
			Logger.comment("setCartId:" + cartid);
			changebean.setCartId(cartid);
			Logger.comment("setIsChecked:" + false);
			changebean.setIsChecked("false");
			Logger.comment("setUserId:" + re.getUid());
			changebean.setUserId(re.getUid());
			changecall.setData(changebean);
			changecall.callService();
			Logger.comment("changecall send:" + changecall.getEntityString());
			Logger.comment("changecall return:" + changecall.getReturnData());
			Logger.verifyEquals(ECode.FAILED201, changecall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, changecall.getBCode(), "验证返回bcode");
			// 应该为 记录不存在?
			Logger.verifyEquals(true, changecall.getMsg().contains(ECode.FAILED201_MSG_7), "验证返回Msg是否包含字符:"
					+ ECode.FAILED201_MSG_7);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeCatalogSelected_007() {
		Logger.start(true,"修改购物车选中状态为选中成功");
		try {
			Logger.comment("从mongodb中随机获取一条购物车记录");

			ShopCartMongoBean re = scw.getRandomShoppingCartInfo();
			Logger.comment("setCartId:" + re.get_id());
			changebean.setCartId(re.get_id());
			Logger.comment("setIsChecked:" + true);
			changebean.setIsChecked("true");
			Logger.comment("setUserId:" + re.getUid());
			changebean.setUserId(re.getUid());
			changecall.setData(changebean);
			changecall.callService();
			Logger.comment("changecall send:" + changecall.getEntityString());
			Logger.comment("changecall return:" + changecall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, changecall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, changecall.getBCode(), "验证返回bcode");
			ShopCartMongoBean scb = scw.getShoppingCartInfoList("{\"_id\":\"" + re.get_id() + "\"}", 0).get(0);
			Logger.verifyEquals("true", scb.getCheck(), "验证返回bcode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}*/
}